﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace DAL
{
     public abstract class BaseDAL<T> where T : Model.BaseModel
    {

        /// <summary>
        /// 定义一个抽象的表名
        /// </summary>
        public abstract string TableName { get; }

        /// <summary>
        /// 定义一个抽象的主键
        /// </summary>
        public abstract Dictionary<string, bool> PkName { get; }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Add(T t, params SqlParameter[] parameter)
        {

            string sql = Common.common.ClassToSQL(t, TableName, PkName, parameter);
            return DBHelper.ExecuteNonQuery(sql)>0;
        }
        public bool DeleteById(T t) 
        {
            string sql = Common.common.ClassTODel(t, TableName, PkName);
            return DBHelper.ExecuteNonQuery(sql) > 0;
        }
        public bool Update(T t, SqlParameter[] parameter)
        {
            string sql = Common.common.ClassToUpdate(t, TableName, PkName, parameter);
            return DBHelper.ExecuteNonQuery(sql) > 0;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<D> GetList<D>(params SqlParameter[] param) where D : new()
        {
            string sql = Common.common.ClassToShow<D>(TableName,param);
            return DBHelper.ExecuteObjects<D>(sql);
        }
         
        
    }

}
